class Constraint 


    def self.method_missing(method_name)
          method_name_array = method_name.to_s.split("_")
          $constraint_add = method_name_array[0]
          $columnname = method_name_array[1..(method_name_array.index("under")-1)].join("_")
          $tablename = method_name_array[(method_name_array.index("under")+1)..(method_name_array.index("as")-1)].join("_")
          $constraint = method_name_array[(method_name_array.index("as")+1)..(method_name_array.length-1)].join("_")
          if method_name_array[1] == "last" && method_name_array[3] == "columns"
            $value = method_name_array[2]
          end  
          return (Constraint.new)
    end  



    def self.create_constraint(option_hash)
          option_hash.each{|key,value|
                if key.to_s != "connection"
                      Constraint.hash_based_constraint_transforms(key,value,option_hash[:connection],"add")
                end      
          }
          puts "Constraints were sucessfullly created."
    end  


    
    def self.hash_based_constraint_transforms(key,value,connection,const_directive)
          if ["primary_key","not_null","unique","auto_increment"].include? key.to_s
              all_columns=[]
              value.each{|inner_key,inner_value|
                    all_columns = connection.query("desc #{inner_key.to_s}")
                    all_columns = get_columns_from_numbers(inner_value,all_columns) 
                    table_name = inner_key
                    all_columns.each{|column_name|
                        if const_directive == "add"  
                                add_the_constraint(inner_key,column_name[0],key.to_s,connection)
                        elsif  const_directive == "drop"        
                                drop_the_constraint(inner_key,column_name[0],key.to_s,connection)
                        end        
              }
            }    
          end
    end  
  
  
  
    def self.drop_constraint(option_hash)
            option_hash.each{|key,value|
                if key.to_s != "connection"
                      Constraint.hash_based_constraint_transforms(key,value,option_hash[:connection],"drop")
                end      
          }
          puts "Constraints were sucessfullly dropped."
    end



    def with connection
          if $value != nil
              all_columns = connection.query("desc #{$tablename}")
              all_required_columns = get_columns_from_positions($value,all_columns)
              for column in all_required_columns
                  Constraint.execute_constraints(column,connection) 
              end
          else  
              Constraint.execute_constraints($columnname,connection) 
          end      
    end  
        


    def self.execute_constraints(column,connection) 
        if $constraint_add == "make"
            add_the_constraint($tablename,column,$constraint,connection) 
        elsif $constraint_add == "drop"
            drop_the_constraint($tablename,column,$constraint,connection)
        end    
    end

end  